`timescale 1ns / 1ps
//////////////////////////////////////////////////////////////////////////////////
// Company: 
// Engineer: Rohit Kulkarni, Vedang Vaidya
// 
// Create Date:    20:33:09 04/09/2014 
// Design Name: 
// Module Name:    nexys_bot_if 
// Project Name: 
// Target Devices: 
// Tool versions: 
// Description: 
//
// Dependencies: 
//
// Revision: 
// Revision 0.01 - File Created
// Additional Comments: 
//
//////////////////////////////////////////////////////////////////////////////////
module nexys_bot_if(
	//Interfaces to kcpsm6
	input wire	[7:0]		port_id,
	input wire	[7:0]		out_port,
	output reg	[7:0]		in_port,
	input wire			write_strobe,
	input wire			k_write_strobe,
	input wire			read_strobe,
	output reg			interrupt = 1'b0,	          //See note above
	input wire			interrupt_ack,
	
	//Interfaces to BotSim
	output reg	[7:0] motctl,
	input wire	[7:0] locX,
	input wire	[7:0] locY,
	input wire	[7:0] botinfo,
	input wire	[7:0] sensors,
	input wire	[7:0] lmdist,
	input wire	[7:0] rmdist,
	input wire	 upd_sysregs,
	
	//Interfaces to 7 segment display
	output reg [4:0] dig3,
	output reg [4:0] dig2,
	output reg [4:0] dig1,
	output reg [4:0] dig0,
	output reg [3:0] dp,

	//Interfaces to debounce logic
	input wire [4:0] db_btns,	
	input wire [7:0] db_sw,
	
	//Interface to LEDs
	output reg	[7:0]	led,
	
	input clk
);

	//Internal variables
/*
	reg [7:0] MOTCTL;
	reg [7:0] LOCX;
	reg [7:0] LOCY;	
	reg [7:0] BOTINFO;
	reg [7:0] SENSORS;
	reg [7:0] LMDIST;
	reg [7:0] RMDIST;
	reg UPD_SYSREGS;
	reg INTERRUPT_FF;
	reg [4:0] DB_BTNS;
	reg [7:0] DB_SW;
	reg [7:0] LEDS;
	reg [7:0] DIG3;	
	reg [7:0] DIG2;
	reg [7:0] DIG1;
	reg [7:0] DIG0;
	reg [7:0] DP;
	reg int_interrupt;
*/

	//Parameters
	parameter	PA_PBTNS			=	8'h00;		// (i) pushbuttons and rotary encoder direction and pushbutton
	parameter	PA_SLSWTCH		=	8'h01;		// (i) slide switches
	parameter	PA_LOCX			=	8'h0A;		// (i) X coordinate of rojobot location
	parameter	PA_LOCY			=	8'h0B;		// (i))Y coordinate of rojobot location
	parameter	PA_BOTINFO		=	8'h0C;		// (i) Rojobot info register
	parameter	PA_SENSORS		=	8'h0D;		// (i) Sensor register
	parameter	PA_LMDIST		=	8'h0E;		// (i) Rojobot left motor distance register
	parameter	PA_RMDIST		=	8'h0F;		// (i) Rojobot right motor distance register

	parameter	PA_LEDS			=	8'h02;		// (o) LEDs
	parameter	PA_DIG3			=	8'h03;		// (o) digit 3 PA address
	parameter	PA_DIG2			=	8'h04;		// (o) digit 2 PA address
	parameter	PA_DIG1			=	8'h05;		// (o) digit 1 PA address
	parameter	PA_DIG0			=	8'h06;		// (o) digit 0 PA address
	parameter	PA_DP				=	8'h07;		// (o) decimal points PA address
	parameter	PA_RSVD			=	8'h08;		// (o) *RESERVED*
	parameter	PA_MOTCTL		=	8'h09;		//	(o) Rojobot motor control output from system
/*		
	parameter	PBTN_CENTER		=	DB_BTNS[0];
	parameter	PBTN_LFTFW		=	DB_BTNS[4];
	parameter	PBTN_LFTRV		=	DB_BTNS[3];
	parameter	PBTN_RGTFW		=	DB_BTNS[2];
	parameter	PBTN_RGTRV		=	DB_BTNS[1];
*/
/*	
	//Latch incoming signals before routing to Picoblaze
	always @(posedge clk)
	begin
		motctl			<=	MOTCTL;
		LOCX				<=	locX;
		LOCY				<=	locY;	
		BOTINFO			<=	botinfo;
		SENSORS			<=	sensors;
		LMDIST			<=	lmdist;
		RMDIST			<=	rmdist;
		INTERRUPT_FF	<=	upd_sysregs;
		DB_BTNS			<=	db_btns;
		DB_SW				<=	db_sw;
	end
*/				

	/*
		Output data to KCPSM6 on the in-port
	*/	
	always @ (posedge clk)
	begin

		case (port_id[7:0]) 
		
		  PA_PBTNS : in_port <= {1'b0,1'b0,1'b0,db_btns[0],db_btns[4],db_btns[3],db_btns[2],db_btns[1]};

		  PA_SLSWTCH : in_port <= db_sw;

		  PA_LOCX : in_port <= locX;

		  PA_LOCY : in_port <= locY;

		  PA_BOTINFO : in_port <= botinfo;

		  PA_SENSORS : in_port <= sensors;

		  PA_LMDIST : in_port <= lmdist;

		  PA_RMDIST: in_port <= rmdist;		  
		  		  
		  default : in_port <= 8'bXXXXXXXX ;  
		endcase
	end
		
		
	/*
		Input data from KCPSM6		
	*/	
	always @ (posedge clk)
	begin

		// 'write_strobe' is used to qualify all writes to general output ports.
		if (write_strobe == 1'b1) begin

		  if (port_id == PA_LEDS) begin
			 led <= out_port;
		  end

		  if (port_id == PA_DIG3) begin
			 dig3 <= out_port;
		  end

		  if (port_id == PA_DIG2) begin
			 dig2 <= out_port;
		  end

		  if (port_id == PA_DIG1) begin
			 dig1 <= out_port;
		  end

		  if (port_id == PA_DIG0) begin
			 dig0 <= out_port;
		  end

		  if (port_id == PA_DP) begin
			 dp <= out_port;
		  end
		  
		  if (port_id == PA_MOTCTL) begin
			 motctl <= out_port;
		  end		  
		end
	end
		
/*		
	//	For constant optimized ports		
		
	always @ (posedge clk)
	begin
		// 'k_write_strobe' is used to qualify all writes to constant output ports.
		if (k_write_strobe == 1'b1) begin

		  // Write to output_port_k at port address 01 hex
		  if (port_id[0] == 1'b1) begin
			 output_port_k <= out_port;
		  end

		  // Write to output_port_c at port address 02 hex
		  if (port_id[1] == 1'b1) begin
			 output_port_c <= out_port;
		  end
		end
	end
*/		
		
	/*
		Closed loop control using interrupt
	*/			
	always @ (posedge clk)
	begin
		if (interrupt_ack == 1'b1) begin
			interrupt <= 1'b0;
		end
		else if (upd_sysregs == 1'b1) begin
			 interrupt <= 1'b1;
		end
		else begin
			 interrupt <= interrupt;
		end
	end
	
/*	
	always @(posedge clk)
	begin
		interrupt	<=	int_interrupt;
	end
*/	
/*
	//Update display for digit 3
	always @(posedge clk)
	begin
		dig3	<=	DIG3[4:0];
	end

	//Update display for digit 2
	always @(posedge clk)
	begin
		dig2	<=	DIG2[4:0];
	end

	//Update display for digit 1
	always @(posedge clk)
	begin
		dig1	<=	DIG1[4:0];
	end

	//Update display for digit 0
	always @(posedge clk)
	begin
		dig0	<=	DIG0[4:0];
	end

	//Update LEDs
	always @(posedge clk)
	begin
		led	<=	SENSORS;
	end

	//Update LEDs
	always @(posedge clk)
	begin
		dp	<=	DP[3:0];
	end
	
*/	
endmodule
